//******************************************************************/
//版本说明:
//V0.1		2011-10-24	jzhang	接收卡生产检测母版程序
//******************************************************************/
module SL909_P01(
		input	wire		sys_resetb,
       	 	input	wire		sclkin,
       	 	
//          	//面板输出
//       	output	wire		out0,
//       	output	wire		out1,
//       	output	wire		out2,
//       	output	wire		out3,
		//数码管显示
		output	wire		sd,
		output	wire		srclk,
		output	wire		rclk,
		output	wire		srclr_n,
		output	wire		oe1_n,
		output	wire		oe2_n,
		output	wire		oe3_n,
		output	wire		oe4_n,
		output	wire		oe5_n,
		//output	wire		oe6_n,
//       	//未定义输出信号
//       	output	wire		ctl1,
//       	output	wire		ctl2,
//       	output	wire		ctl3,
//       	output	wire		ctl4,
       		//按键信号输入      	
		output	wire		sh_ld_0,
		output	wire		p_s_clk,
		output	wire		clk_inh,
		input	wire		qh,
		//output	wire		sh_ld_1,
		//LED控制信号
		//output	wire		led1,
		//output	wire		led2,
		//output	wire		led3,
		//output	wire		led4,
		//结果反馈信号
		input	wire		sdram_rslt,//res0,//SDRAM C_CLK n5
		input	wire		phy_rslt,//PHY C_MISO L6
		input	wire		hub_sync,//res2,//hub_SYNC  C_MOSI R3
		input	wire		no_rslt,//res3,// NO_RESULT C_CS p3

		//继电器控制
		output	wire		power_ctl,
                //本地FLASH接口
		//input	wire		local_flash_SO,      
                //output	wire		local_flash_SCK,     
                //output	wire		local_flash_SI,
                //output	wire		local_flash_CS_n,      

		//fa_oe		t12	PROG_B
		//fa_ex2	l11	F_WP
		//fa_ex4	k10	F_CLK
		//fa_ex6	k9	F_MISO
		//fa_le1	r11	F_MOSI
		//fa_le2	r10	F_CS
		//
		//fa_ex1	m11	key_1	
		//fa_clk	r12	key_2	

		//被测板FLASH接口
		//output	wire		local_fo_oe,		
		input	wire		test_flash_SO,     	//k9 
                inout	tri		test_flash_SCK,     	//k10
                inout	tri		test_flash_SI,		//r11
                inout	tri		test_flash_CS_n,    	//r10
		output  wire  		test_flash_WP_n,	//l11	
		output  wire  		test_flash_PROGB_n,//使用OE6作为prog_b输出	//t12
		
//		input	wire		key_1,
//		input	wire		key_2,
//		input	wire	[39:0]	porta,
//		input	wire	[39:0]	portb,
		input	wire		b_oeb0_7,//a1_a//jp611
		input	wire		b_nc0_7,//a1_b
		input	wire		a_oeb1_7,//a1_c
		input	wire		a_lat1_7,//a1_d
		input	wire		a_clk1_7,//a1_clk
		input	wire		b_c0_7,//a1_oeb
		input	wire		b_d0_7,//a1_lat	
		
		input	wire		b_oeb1_7,//A2_Ajp608
		input	wire		b_nc1,//a2_b
		input	wire		a_oeb3_7,//A2_C
		input	wire		a_lat3_7,//a2_d
		input	wire		a_clk3_7,//a2_clk
		input	wire		b_clk0_7,//a2_oeb
		input	wire		b_lat0_7,//a2_lat
		
		output	wire		pen_1,
		output	wire		pen_2,
		
		output	wire		norn,
		
		output	wire		led_g,
		output	wire		led_r
		);

//******************************************************************/
//			   参数定义
//******************************************************************/
parameter	Sim_Mode	=0;
parameter	START		=2000;

//****************************************************************
//		内部信号
//****************************************************************
wire		key_in_1,key_in_2,key_in_3,key_in_4;
wire		sw_in_1,sw_in_2,sw_in_3,sw_in_4;
wire		block_link;
wire		test_result;
wire		sclk;
wire		pll_reset,resetb;
wire		time_1ms,time_125ms,time_250ms;
wire		time_1ms_sync,time_16us_sync,time_15ms_sync,time_250ms_sync,time_1s_sync;


wire		local_rd_start,local_rd_end,local_rd_d_ok;
wire	[23:0]	local_rd_start_addr;
wire	[7:0]	local_rd_data;

wire		test_wr_flag,test_wr_start,test_wr_end,test_erase_end,test_rd_start,test_rd_end,test_rd_d_ok;
wire	[31:0]	test_wr_start_addr;
wire	[7:0]	test_buf_raddr,test_buf_rdata,test_rd_data;
wire	[23:0]	test_rd_start_addr;

wire		prog_end_flag;
//reg		prog_start_flag;
reg		start;
reg	[12:0]	cnt;
wire	[10:0]	tout;

//共用母版读flash信号
wire		d_local_rd_start;
wire	[23:0]	d_local_rd_start_addr;
wire		d_local_rd_end;
wire	[7:0]	d_local_rd_data;
wire		d_local_rd_d_ok;

wire		ic_local_rd_start;
wire	[23:0]	ic_local_rd_start_addr;
wire		ic_local_rd_end;
wire	[7:0]	ic_local_rd_data;
wire		ic_local_rd_d_ok;
//共用被测板写flash信号
wire		SR_test_wr_flag;
wire		SR_test_wr_start;
wire	[31:0]	SR_test_wr_start_addr;

wire		d_test_wr_flag;
wire		d_test_wr_start;
wire	[31:0]	d_test_wr_start_addr;

//
wire		initial_check_start,initial_check_en;
wire		test_prog_start_flag,test_prog_start;
wire		init_prog_start_flag,init_prog_start;
wire		active_prog_start_flag,active_prog_start;
wire		clean_flag,clean_start,clean_end;
wire		write_disable_flag,write_disable_start,write_disable_end;
wire		write_SR_end;

wire		check_ok,check_err;

wire		test_sdram,test_phy,test_hub,test_flash_err;
wire		test_crc_err_en;
wire		wait_state_en;	
wire	[16:0]	display_num;
wire		wait_result_en;
wire		prog_version_en;
wire		led_r_flash;

wire		test_flash_SCK_temp;     
wire		test_flash_SI_temp;
wire		test_flash_CS_n_temp;

wire		hub_result;

reg	[3:0]	sw;

wire		wait_rslt_flag;
wire		clear_flag;

reg	[3:0]	test_type;
reg	[3:0]	delay_cnt;



//7型卡端口


//**************************************************************
//		时钟复位管理单元
//**************************************************************
//in            sclkin,sys_resetb
//out           sclk,resetb      

//out           time_1ms_sync,time_1ms

//sys_clk sys_clk (
//		    .CLKIN_IN(sclkin), 
//		    .RST_IN(pll_reset), 
//		    .CLKIN_IBUFG_OUT(), 
//		    .CLK0_OUT(sclk), 
//		    .LOCKED_OUT()
//    );

always @(posedge sclk or negedge resetb)
	if(resetb==1'b0)
		delay_cnt<='d0;
	else if(power_ctl==1'b0)
		delay_cnt<='d0;
	else if(power_ctl==1'b1 && delay_cnt<'d10 && time_1ms_sync==1'b1)
		delay_cnt<=delay_cnt+1'b1;

clk_gen	clk_gen_inst (
	.inclk0 ( sclkin ),
	.c0 ( sclk ),
	.locked (  )
	);

assign	norn=1'b0;
assign	pen_1=(delay_cnt=='d10)?1'b0:1'b1; 
assign	pen_2=1'b0;  
assign  resetb=sys_resetb;
assign  pll_reset=~sys_resetb;

sys_timer       sys_timer(
                .resetb(resetb),
                .sclk(sclk),
                
                .time_1ms(time_1ms),
                .time_250ms(time_250ms),
                .time_125ms(time_125ms),
                
                .time_1ms_sync(time_1ms_sync),
                .time_16us_sync(time_16us_sync),
                .time_15ms_sync(time_15ms_sync),
                .time_250ms_sync(time_250ms_sync),
                .time_1s_sync(time_1s_sync)   
                );
                
epcs_io epcs_io(
	.asdo_in(local_flash_SI),
	.asmi_access_granted(1'b1),
	.dclk_in(local_flash_SCK),
	.ncso_in(local_flash_CS_n),
	.noe_in(1'b0),
	.asmi_access_request(),
	.data0_out(local_flash_SO));
//**************************************************************
//		器件信号控制
//**************************************************************
//assign	local_fo_oe=1;


//always@(posedge sclk or negedge resetb)
//	if(resetb==0)
//		cnt<=0;
//	else if(cnt[12]==0 && time_1ms_sync==1)
//		cnt<=cnt+1;
//
//
//always@(posedge sclk)
//	if(cnt==START && time_1ms_sync==1)
//		start<=1;
//	else
//		start<=0;


//**************************************************************
//		整体控制
//**************************************************************
//led状态指示
Led_Ctrl_SV2	Led_Ctrl_SV2_local(
		.resetb(resetb),
		.sclk(sclk),
		
		.time_ms(time_1ms),
                .time_250ms(time_250ms),

		.G_black(led_g_black),
		.G_flash(led_g_flash),

		.R_light(1'b0),
		.R_flash(1'b0),//1'b0),

		.G_flash_1st(1'd1),
		.R_flash_1st(1'd0),

		.nG_led(led_g),
		.nR_led(led_r)
		);
//背板状态指示灯		
//Led_Ctrl_SV2	Led_Ctrl_SV2_back_1(
//		.resetb(resetb),
//		.sclk(sclk),
//		
//		.time_ms(time_1ms),
//                .time_250ms(time_250ms),
//
//		.G_black(1'b1),
//		.G_flash(test_sdram),
//
//		.R_light(1'b0),
//		.R_flash(test_phy),
//
//		.G_flash_1st(1'd1),
//		.R_flash_1st(1'd1),
//
//		.nG_led(led1_bk),
//		.nR_led(led2_bk)
//		);
assign	led1=~test_sdram;	
assign	led2=~test_phy;	
		
//Led_Ctrl_SV2	Led_Ctrl_SV2_back_2(
//		.resetb(resetb),
//		.sclk(sclk),
//		
//		.time_ms(time_1ms),
//                .time_250ms(time_250ms),
//
//		.G_black(1'b1),
//		.G_flash(test_flash_err),
//
//		.R_light(1'b0),
//		.R_flash(test_hub),
//
//		.G_flash_1st(1'd1),
//		.R_flash_1st(1'd1),
//
//		.nG_led(led3_bk),
//		.nR_led(led4_bk)
//		);
assign	led3=~test_flash_err;	
assign	led4=~test_hub;			

wire		led_g_flash,led_g_black;
main_ctrl	main_ctrl(
			.sclk(sclk),
			.resetb(resetb),
			
			//.start(start),
			.key_in_1(sw_in_2),
			.key_in_2(sw_in_4),
			.block_link(block_link),
			
			.time_1ms_sync(time_1ms_sync),
			
			.test_ok(test_ok),
			.test_end(test_end),
			
			.led_g_black(led_g_black),
			.led_g_flash(led_g_flash),
			.led_r_flash(led_r_flash),
			.power_ctl(power_ctl),
			.test_flash_PROGB_n(test_flash_PROGB_n),
			.test_flash_WP_n(test_flash_WP_n),
			
			//crc检验结果信号
			.check_ok(check_ok),
			.check_err(check_err),
			.prog_end_flag(prog_end_flag),
			
			//crc检验控制信号
			.initial_check_start(initial_check_start),
			.initial_check_en(initial_check_en),
			.wait_state_en(wait_state_en),
			.test_prog_start_flag(test_prog_start_flag),
			.test_prog_start(test_prog_start),
			.wait_result_en(wait_result_en),
			.init_prog_start_flag(init_prog_start_flag),
			.init_prog_start(init_prog_start),
			.active_prog_start_flag(active_prog_start_flag),
			.active_prog_start(active_prog_start),
			.prog_version_en(prog_version_en),
			
			//控制被测板状态寄存器
			.clean_flag(clean_flag),//一个周期信号
			.clean_start(clean_start),
			.write_disable_flag(write_disable_flag),
			.write_disable_start(write_disable_start),
			.write_SR_end(write_SR_end),
			
			.wait_rslt_flag(wait_rslt_flag),
			//数码管控制信号
			.display_num(display_num),
			
			//被测板flash crc校验错误
			.test_crc_err_en(test_crc_err_en)
			
			
			);
			
//**************************************************************
//		 按键处理逻辑模块
//**************************************************************	
always @(posedge sclk or negedge resetb)
	if(resetb==1'b0)
		sw<='d0;
	else sw<={sw_in_4,sw_in_3,sw_in_2,sw_in_1};	
	
capture_button	capture_button(
				.sclk(sclk),
				.resetb(resetb),

				//采集后的按键接口。低有效
				//.key_in_1(key_in_1),
				.key_in_2(key_in_2),
				.key_in_3(key_in_3),
				.key_in_4(key_in_4),
				.block_link(block_link),
				.sw_in_1(sw_in_1),
				.sw_in_2(sw_in_2),
				.sw_in_3(sw_in_3),
				.sw_in_4(sw_in_4),
								
				//74hc165接口
				.load_n_0(sh_ld_0),
				//.load_n_1(sh_ld_1),
				.p_s_clk(p_s_clk),
				.clk_inh(clk_inh),
				.qh(qh)
				);
							
//**************************************************************
//		 按键处理逻辑模块
//**************************************************************
always @(posedge sclk or negedge resetb)
	if(resetb==1'b0)
		test_type<='d0;
	else case(sw)
		4'd0:test_type<=4'd1;
		4'd1:test_type<=4'd2;
		4'd3:test_type<=4'd7;
		default:test_type<='d0;
	endcase
out_ctrl	out_ctrl(
			.sclk(sclk),
			.resetb(resetb),
			
			//输入显示数据控制
			.display_num_0(display_num[3:0]),
			.display_num_1(display_num[7:4]),
			.display_num_2(display_num[11:8]),
			.display_num_3(test_type),//display_num[15:12]),
			.display_en(display_num[16]),
			.led1(led1),
			.led2(led2),
			.led3(led3),
			.led4(led4),
			//输出74HC595控制
			.shift_load(rclk),
			.shift_clk(srclk),
			.oe_1(oe1_n),
			.oe_2(oe2_n),
			.oe_3(oe3_n),
			.oe_4(oe4_n),
			.oe_5(oe5_n),
			.shift_data(sd)
			);
assign		srclr_n=1'b1;

check_port	check_port(
			.sclk(sclk),
			.resetb(resetb),
			.sw(sw),
	
			.wait_state_en(wait_rslt_flag),
			.ms_flag(time_1ms_sync),
			
			.b_oeb0_7(b_oeb0_7),//a1_a//jp611
			.b_nc0_7(b_nc0_7),//a1_b
			.a_oeb1_7(a_oeb1_7),//a1_c
			.a_lat1_7(a_lat1_7),//a1_d
			.a_clk1_7(a_clk1_7),//a1_clk
			.b_c0_7(b_c0_7),//a1_oeb
			.b_d0_7(b_d0_7),//a1_lat	
			
			.b_oeb1_7(b_oeb1_7),//A2_Ajp608
			.b_nc1(b_nc1),//a2_b
			.a_oeb3_7(a_oeb3_7),//A2_C
			.a_lat3_7(a_lat3_7),//a2_d
			.a_clk3_7(a_clk3_7),//a2_clk
			.b_clk0_7(b_clk0_7),//a2_oeb
			.b_lat0_7(b_lat0_7),//a2_lat
	
			.porta(),
			.portb(),
			.sync(hub_sync),
			.clear_flag(clear_flag),
			.hub_result(hub_result)
);

//**************************************************************
//		 反馈信号处理逻辑
//**************************************************************
res_clk_top	res_clk_top(
			.sclk(sclk),
			.resetb(resetb),
			
			.time_1ms_sync(time_1ms_sync),
			.time_16us_sync(time_16us_sync),
			.wait_result_en(wait_result_en),
			.test_crc_err_en(test_crc_err_en),
			.wait_state_en(wait_state_en),
			.res0(sdram_rslt),
			.res1(phy_rslt),
			.res2(hub_result),
			.res3(no_rslt),
			
			.test_sdram(test_sdram),
			.test_phy(test_phy),
			.test_flash_err(test_flash_err),
			.test_hub(test_hub),
			.test_ok(test_ok),	//1是ok，0是error	
			.test_end(test_end)	
			);

	
//**************************************************************
//		 CRC校验及控制
//**************************************************************
crc_check_01	crc_check_01(
			.resetb(resetb),
			.sclk(sclk),
			.time_1ms_sync(time_1ms_sync),
		
			.initial_check_start(initial_check_start),
			
			.prog_end_flag(prog_end_flag),
			.test_prog_start(test_prog_start),
			.init_prog_start(init_prog_start),
			.active_prog_start(active_prog_start),
		
			.local_rd_start(ic_local_rd_start),
			.local_rd_start_addr(ic_local_rd_start_addr),
			.local_rd_end(ic_local_rd_end),
			.local_rd_data(ic_local_rd_data),
			.local_rd_d_ok(ic_local_rd_d_ok),
			
			.test_rd_start(test_rd_start),
			.test_rd_start_addr(test_rd_start_addr),
			.test_rd_end(test_rd_end),
			.test_rd_data(test_rd_data),
			.test_rd_d_ok(test_rd_d_ok),
		
			.check_ok(check_ok),
			.check_err(check_err)
		
			);
			
//**************************************************************
//		 读母版flash数据bus
//**************************************************************	
local_rd_bus	local_rd_bus(
			.sclk(sclk),
			.resetb(resetb),
			//母版读控制
			.initial_check_en(initial_check_en),
			.test_prog_start(test_prog_start),
			.init_prog_start(init_prog_start),
			.active_prog_start(active_prog_start),
			.prog_version_en(prog_version_en),
			
			.ic_local_rd_start(ic_local_rd_start),
			.ic_local_rd_start_addr(ic_local_rd_start_addr),
			.ic_local_rd_end(ic_local_rd_end),
			.ic_local_rd_data(ic_local_rd_data),
			.ic_local_rd_d_ok(ic_local_rd_d_ok),
			
			.d_local_rd_start(d_local_rd_start),
			.d_local_rd_start_addr(d_local_rd_start_addr),
			.d_local_rd_end(d_local_rd_end),
			.d_local_rd_d_ok(d_local_rd_d_ok),
			.d_local_rd_data(d_local_rd_data),
			
			.local_rd_start(local_rd_start),
			.local_rd_start_addr(local_rd_start_addr),
			.local_rd_end(local_rd_end),
			.local_rd_d_ok(local_rd_d_ok),
			.local_rd_data(local_rd_data)
			
			);
		
//**************************************************************
//		 写被测板flash数据bus
//**************************************************************
			
test_wr_bus	test_wr_bus(
			.sclk(sclk),
			.resetb(resetb),
			//母版读控制
			.clean_start(clean_start),
			.write_disable_start(write_disable_start),
			
			.SR_test_wr_flag(SR_test_wr_flag),
			.SR_test_wr_start(SR_test_wr_start),
			.SR_test_wr_start_addr(SR_test_wr_start_addr),
			
			.d_test_wr_flag(d_test_wr_flag),
			.d_test_wr_start(d_test_wr_start),
			.d_test_wr_start_addr(d_test_wr_start_addr),
			
			.test_wr_flag(test_wr_flag),
			.test_wr_start(test_wr_start),
			.test_wr_start_addr(test_wr_start_addr)
			
			);
			
//**************************************************************
//	清除被测板FLASH状态寄存器，写保护被测板FLASH
//**************************************************************
write_SR_01	write_SR_01(
			.sclk(sclk),
			.resetb(resetb),
			.time_1ms_sync(time_1ms_sync),
			
			.clean_flag(clean_flag),
			.clean_end(clean_end),
			.write_disable_flag(write_disable_flag),
			.write_disable_end(write_disable_end),
			
			.test_wr_flag(SR_test_wr_flag),
			.test_wr_start(SR_test_wr_start),
			.test_wr_start_addr(SR_test_wr_start_addr),
			.write_SR_end(write_SR_end)	

			);
//**************************************************************
//		读母版FLASH，写被测板FLASH通讯控制
//**************************************************************
			
prog_download_01 prog_download(
			.sclk(sclk),
			.resetb(resetb),
			
			.test_prog_start_flag(test_prog_start_flag),
			.init_prog_start_flag(init_prog_start_flag),
			.active_prog_start_flag(active_prog_start_flag),
			.prog_version_en(prog_version_en),
			
			.prog_end_flag(prog_end_flag),
			
			.time_1ms_sync(time_1ms_sync),
			
			.local_rd_start(d_local_rd_start),
			.local_rd_start_addr(d_local_rd_start_addr),
			.local_rd_end(d_local_rd_end),
			.local_rd_d_ok(d_local_rd_d_ok),
			.local_rd_data(d_local_rd_data),	
			
			.test_wr_flag(d_test_wr_flag),
			.test_wr_start(d_test_wr_start),
			.test_wr_start_addr(d_test_wr_start_addr),
			.erase_end(test_erase_end),
			.test_wr_end(test_wr_end),
			.test_buf_raddr(test_buf_raddr),
			.test_buf_rdata(test_buf_rdata),
			
			.tout()
			);
			
			
//**************************************************************
//		FLASH接口模块
//**************************************************************
//本地FLASH控制
v8_flash_ctrl_02 local_flash_ctrl(
		.resetb(resetb),
		.sclk(sclk),

		//和FLASH通讯控制的接口
		.fpga_rec_flag(),
		.fpga_send_flag(),
		.op_start_flag(),
		.op_start_addr(),
		.op_length(),
		
		.rec_end(),
		.rec_buf_raddr(),
		.rec_buf_rdata(),		

		.send_end(),
		.send_buf_we(),
		.send_buf_waddr(),
		.send_buf_wdata(),	

		.read_start(local_rd_start),
		.start_addr(local_rd_start_addr),
		.read_end(local_rd_end),
		
		.read_d_ok(local_rd_d_ok),
		.read_d_ok_d(),
		.read_data(local_rd_data),

		//和flash接口
	        .flash_SO(local_flash_SO),
		.flash_SCK(local_flash_SCK),
		.flash_SI(local_flash_SI),
		.flash_CS_n(local_flash_CS_n),

        	.tout()
		);

//被测板FLASH控制
v8_flash_ctrl_02 test_flash_ctrl(
		.resetb(resetb),
		.sclk(sclk),

		//和通讯模块接口
		.fpga_rec_flag(test_wr_flag),
		.fpga_send_flag(),
		.op_start_flag(test_wr_start),
		.op_start_addr(test_wr_start_addr),
		.op_length(),
		
		.erase_end(test_erase_end),

		.rec_end(test_wr_end),
		.rec_buf_raddr(test_buf_raddr),
		.rec_buf_rdata(test_buf_rdata),		

		.send_end(),
		.send_buf_we(),
		.send_buf_waddr(),
		.send_buf_wdata(),	

		//
		.read_start(test_rd_start),
		.start_addr(test_rd_start_addr),
		.read_end(test_rd_end),
		
		.read_d_ok(),
		.read_d_ok_d(test_rd_d_ok),
		.read_data(test_rd_data),
		//写保护接口
		.clean_end(clean_end),
		.write_disable_end(write_disable_end),	

		//和flash接口
	        .flash_SO(test_flash_SO),
		.flash_SCK(test_flash_SCK_temp),
		.flash_SI(test_flash_SI_temp),
		.flash_CS_n(test_flash_CS_n_temp),

        	.tout()
		);
assign	test_flash_SCK=(test_flash_PROGB_n=='d0)?test_flash_SCK_temp:'bz;
assign	test_flash_SI=(test_flash_PROGB_n=='d0)?test_flash_SI_temp:'bz;
assign	test_flash_CS_n=(test_flash_PROGB_n=='d0)?test_flash_CS_n_temp:'bz;
		                    
//**************************************************************
//		测试接口
//**************************************************************
assign	tout=0;

endmodule